#!/usr/bin/env python

import sys,pygtk,gtk,gtk.glade,os,ConfigParser,commands,gobject,pynotify,shutil,time
import subprocess as sp

class advPrefs:
    "This is the starting dialog that asks initial preferences."
    
    def IntUp(self, widget):
        self.which = self.IntUpCombo.get_active_text()
        if not self.which == "":
            os.system("gksudo ifconfig %s up" % (self.which,))
        if Config.getboolean("config", "notifications"): 
            pynotify.init("WattOS Power Manager") 
            n = pynotify.Notification("Interface %s up" % (self.which,)) 
            n.attach_to_status_icon(icon) 
            n.show() 
        
    def IntDown(self, widget):
        self.which = self.IntUpCombo.get_active_text()
        if not self.which == "":
            os.system("gksudo ifconfig %s down" % (self.which,))
        if Config.getboolean("config", "notifications"): 
            pynotify.init("WattOS Power Manager") 
            n = pynotify.Notification("Interface %s down" % (self.which,)) 
            n.attach_to_status_icon(icon) 
            n.show() 
    
    def __init__(self):
        "Display the advance preferences window."
        
        self.gladefile = "/usr/share/wattospm/wattospm.glade"
        self.wTree = gtk.glade.XML (self.gladefile)
        dic = { "onPrefsCancelClicked" : self.PrefsCancelClicked ,
                    "onPrefsOKClicked" : self.PrefsOKClicked,
                    "onRestoreDefaultsClicked": self.RestoreDefaultsClicked,
                    "onIntUpUpClicked": self.IntUp,
                    "onIntUpDownClicked": self.IntDown }
        self.wTree.signal_autoconnect(dic)
        self.advPrefsWindow = self.wTree.get_widget("advPrefsWindow")
        
        #general tab
        self.username = self.wTree.get_widget("UsernameTE")
        self.rootDrive = self.wTree.get_widget("RootDriveTE")
        self.rootPartition = self.wTree.get_widget("RootPartitionTE")
        self.cdrom = self.wTree.get_widget("cdromTE")
        self.interfaces = self.wTree.get_widget("InterfacesTE")
        self.firstRun = self.wTree.get_widget("FirstRunCB")
        self.smp = self.wTree.get_widget("smpCB")
        self.notifications = self.wTree.get_widget("NotificationsCB")
        
        #disks tab
        self.DRatioDe = self.wTree.get_widget("DRatioDe")
        self.DRatioEn = self.wTree.get_widget("DRatioEn")
        self.DRatioSav = self.wTree.get_widget("DRatioSav")
        self.DRatioPerf = self.wTree.get_widget("DRatioPerf")
        
        self.DBRatioDe = self.wTree.get_widget("DBRatioDe")
        self.DBRatioEn = self.wTree.get_widget("DBRatioEn")
        self.DBRatioSav = self.wTree.get_widget("DBRatioSav")
        self.DBRatioPerf = self.wTree.get_widget("DBRatioPerf")
        
        self.DWritebackDe = self.wTree.get_widget("DWritebackDe")
        self.DWritebackEn = self.wTree.get_widget("DWritebackEn")
        self.DWritebackSav = self.wTree.get_widget("DWritebackSav")
        self.DWritebackPerf = self.wTree.get_widget("DWritebackPerf")
        
        self.HDPDe = self.wTree.get_widget("HDPDe")
        self.HDPEn = self.wTree.get_widget("HDPEn")
        self.HDPSav = self.wTree.get_widget("HDPSav")
        self.HDPPerf = self.wTree.get_widget("HDPPerf")
        
        self.JCommitDe = self.wTree.get_widget("JCommitDe")
        self.JCommitEn = self.wTree.get_widget("JCommitEn")
        self.JCommitSav = self.wTree.get_widget("JCommitSav")
        self.JCommitPerf = self.wTree.get_widget("JCommitPerf")
        
        self.SataDe = self.wTree.get_widget("SataDe")
        self.SataEn = self.wTree.get_widget("SataEn")
        self.SataSav = self.wTree.get_widget("SataSav")
        self.SataPerf = self.wTree.get_widget("SataPerf")
        self.SataHosts = self.wTree.get_widget("SataHosts")
        
        self.HALDe = self.wTree.get_widget("HALDe")
        self.HALEn = self.wTree.get_widget("HALEn")
        
        self.ReadADe = self.wTree.get_widget("ReadADe")
        self.ReadAEn = self.wTree.get_widget("ReadAEn")
        self.ReadASav = self.wTree.get_widget("ReadASav")
        self.ReadAPerf = self.wTree.get_widget("ReadAPerf")
        
        #network tab
        self.IWLDe = self.wTree.get_widget("IWLDe")
        self.IWLEn = self.wTree.get_widget("IWLEn")
        self.IWLSav = self.wTree.get_widget("IWLSav")
        self.IWLPerf = self.wTree.get_widget("IWLPerf")
        self.IWLDirs = self.wTree.get_widget("IWLDirs")
        
        self.IPWDe = self.wTree.get_widget("IPWDe")
        self.IPWEn = self.wTree.get_widget("IPWEn")
        self.IPWSav = self.wTree.get_widget("IPWSav")
        self.IPWPerf = self.wTree.get_widget("IPWPerf")
        self.IPWListStore = gtk.ListStore(gobject.TYPE_STRING)
        self.IPWCombo = self.wTree.get_widget("IPWCombo")     
        
        #modules tab
        self.BlueDe = self.wTree.get_widget("BlueDe")
        self.BlueEn = self.wTree.get_widget("BlueEn")
        self.BlueMod = self.wTree.get_widget("BlueMod")
        
        self.USBDe = self.wTree.get_widget("USBDe")
        self.USBEn = self.wTree.get_widget("USBEn")
        self.USBMod = self.wTree.get_widget("USBMod")
        
        self.FireDe = self.wTree.get_widget("FireDe")
        self.FireEn = self.wTree.get_widget("FireEn")
        self.FireMod = self.wTree.get_widget("FireMod")
        
        self.CamDe = self.wTree.get_widget("CamDe")
        self.CamEn = self.wTree.get_widget("CamEn")
        self.CamMod = self.wTree.get_widget("CamMod")
        
        self.TrackerDe = self.wTree.get_widget("TrackerDe")
        self.TrackerEn = self.wTree.get_widget("TrackerEn")
        
        #other tab
        self.ATIDe = self.wTree.get_widget("ATIDe")
        self.ATIEn = self.wTree.get_widget("ATIEn")
        self.ATISav = self.wTree.get_widget("ATISav")
        self.ATIPerf = self.wTree.get_widget("ATIPerf")
        
        self.NVDe = self.wTree.get_widget("NVDe")
        self.NVEn = self.wTree.get_widget("NVEn")
        self.NVSav = self.wTree.get_widget("NVSav")
        self.NVPerf = self.wTree.get_widget("NVPerf")
        
        self.MCDe = self.wTree.get_widget("MCDe")
        self.MCEn = self.wTree.get_widget("MCEn")
        self.MCSav = self.wTree.get_widget("MCSav")
        self.MCPerf = self.wTree.get_widget("MCPerf")
        
        self.WMDe = self.wTree.get_widget("WMDe")
        self.WMEn = self.wTree.get_widget("WMEn")
        self.WMSav = self.wTree.get_widget("WMSav")
        self.WMPerf = self.wTree.get_widget("WMPerf")
        
        self.ASPMDe = self.wTree.get_widget("ASPMDe")
        self.ASPMEn = self.wTree.get_widget("ASPMEn")
        self.ASPMSav = self.wTree.get_widget("ASPMSav")
        self.ASPMPerf = self.wTree.get_widget("ASPMPerf")
        
        self.IntelSDe = self.wTree.get_widget("IntelSDe")
        self.IntelSEn = self.wTree.get_widget("IntelSEn")
        self.IntelSSav = self.wTree.get_widget("IntelSSav")
        self.IntelSPerf = self.wTree.get_widget("IntelSPerf")
        
        self.AC97De = self.wTree.get_widget("AC97De")
        self.AC97En = self.wTree.get_widget("AC97En")
        self.AC97Sav = self.wTree.get_widget("AC97Sav")
        self.AC97Perf = self.wTree.get_widget("AC97Perf")
        
        self.BackDe = self.wTree.get_widget("BackDe")
        self.BackEn = self.wTree.get_widget("BackEn")
        self.BackSav = self.wTree.get_widget("BackSav")
        self.BackPerf = self.wTree.get_widget("BackPerf")
        
        self.ACPITDe = self.wTree.get_widget("ACPITDe")
        self.ACPITEn = self.wTree.get_widget("ACPITEn")
        self.ACPITSav = self.wTree.get_widget("ACPITSav")
        self.ACPITPerf = self.wTree.get_widget("ACPITPerf")
        
        self.NMIDe = self.wTree.get_widget("NMIDe")
        self.NMIEn = self.wTree.get_widget("NMIEn")
        self.NMISav = self.wTree.get_widget("NMISav")
        self.NMIPerf = self.wTree.get_widget("NMIPerf")
        
        # One time option tab
        self.IntUpListStore = gtk.ListStore(gobject.TYPE_STRING)
        self.IntUpCombo = self.wTree.get_widget("IntUpCombo") 
        
        self.readSettings()
        self.advPrefsWindow.show()

    def readSettings(self):
        "read settings from config and display in the advanced preferences window"
        
        #general tab
        self.username.set_text(Config.get("config","username"))
        self.rootDrive.set_text(Config.get("config","root_drive"))
        self.rootPartition.set_text(Config.get("config","root_partition"))
        self.cdrom.set_text(Config.get("config","cdrom"))
        self.interfaces.set_text(Config.get("config","interfaces"))
        self.firstRun.set_active(Config.getboolean("config","first_run"))
        self.smp.set_active(Config.getboolean("config","smp"))
        self.notifications.set_active(Config.getboolean("config","notifications"))
        
        #disks tab
        self.DRatioDe.set_active(Config.getboolean("dirty_ratio", "detected"))
        self.DRatioEn.set_active(Config.getboolean("dirty_ratio", "enabled"))
        self.DRatioSav.set_text(Config.get("dirty_ratio", "saving_setting"))
        self.DRatioPerf.set_text(Config.get("dirty_ratio", "performance_setting"))
        
        self.DBRatioDe.set_active(Config.getboolean("dirty_background_ratio", "detected"))
        self.DBRatioEn.set_active(Config.getboolean("dirty_background_ratio", "enabled"))
        self.DBRatioSav.set_text(Config.get("dirty_background_ratio", "saving_setting"))
        self.DBRatioPerf.set_text(Config.get("dirty_background_ratio", "performance_setting"))
        
        self.DWritebackDe.set_active(Config.getboolean("dirty_writeback", "detected"))
        self.DWritebackEn.set_active(Config.getboolean("dirty_writeback", "enabled"))
        self.DWritebackSav.set_text(Config.get("dirty_writeback", "saving_setting"))
        self.DWritebackPerf.set_text(Config.get("dirty_writeback", "performance_setting"))
        
        self.HDPDe.set_active(Config.getboolean("hdparm", "detected"))
        self.HDPEn.set_active(Config.getboolean("hdparm", "enabled"))
        self.HDPSav.set_text(Config.get("hdparm", "saving_setting"))
        self.HDPPerf.set_text(Config.get("hdparm", "performance_setting"))
        
        self.JCommitDe.set_active(Config.getboolean("journal_commit", "detected"))
        self.JCommitEn.set_active(Config.getboolean("journal_commit", "enabled"))
        self.JCommitSav.set_text(Config.get("journal_commit", "saving_setting"))
        self.JCommitPerf.set_text(Config.get("journal_commit", "performance_setting"))
        
        self.SataDe.set_active(Config.getboolean("sata", "detected"))
        self.SataEn.set_active(Config.getboolean("sata", "enabled"))
        self.SataSav.set_text(Config.get("sata", "saving_setting"))
        self.SataPerf.set_text(Config.get("sata", "performance_setting"))
        self.SataHosts.set_text(Config.get("sata", "hosts"))
        
        self.HALDe.set_active(Config.getboolean("hal_polling", "detected"))
        self.HALEn.set_active(Config.getboolean("hal_polling", "enabled"))
        
        self.ReadADe.set_active(Config.getboolean("readahead", "detected"))
        self.ReadAEn.set_active(Config.getboolean("readahead", "enabled"))
        self.ReadASav.set_text(Config.get("readahead", "saving_setting"))
        self.ReadAPerf.set_text(Config.get("readahead", "performance_setting"))
        
        #network tab
        self.IWLDe.set_active(Config.getboolean("iwl_wifi", "detected"))
        self.IWLEn.set_active(Config.getboolean("iwl_wifi", "enabled"))
        self.IWLSav.set_text(Config.get("iwl_wifi", "saving_setting"))
        self.IWLPerf.set_text(Config.get("iwl_wifi", "performance_setting"))
        self.IWLDirs.set_text(Config.get("iwl_wifi", "dirs"))
        
        self.IPWDe.set_active(Config.getboolean("ipw_wifi", "detected"))
        self.IPWEn.set_active(Config.getboolean("ipw_wifi", "enabled"))
        self.IPWSav.set_text(Config.get("ipw_wifi", "saving_setting"))
        self.IPWPerf.set_text(Config.get("ipw_wifi", "performance_setting"))
        self.ipw_interfaces = Config.get("config", "interfaces").split(",")
        for self.interface in self.ipw_interfaces:
            self.row=self.IPWListStore.insert_after(None, None)
            self.IPWListStore.set_value(self.row, 0, self.interface)
        self.IPWCombo.set_model(self.IPWListStore)
        self.cell=gtk.CellRendererText()
        self.IPWCombo.pack_start(self.cell)
        self.IPWCombo.add_attribute(self.cell, 'text', 0)
        self.IPWCombo.set_wrap_width(1)
        if Config.get("ipw_wifi","interface"):
            self.IPWCombo.set_active(self.ipw_interfaces.index(Config.get("ipw_wifi","interface")))
        
        #modules tab
        self.BlueDe.set_active(Config.getboolean("bluetooth", "detected"))
        self.BlueEn.set_active(Config.getboolean("bluetooth", "enabled"))
        self.BlueMod.set_text(Config.get("bluetooth", "modules"))
        
        self.USBDe.set_active(Config.getboolean("usb", "detected"))
        self.USBEn.set_active(Config.getboolean("usb", "enabled"))
        self.USBMod.set_text(Config.get("usb", "modules"))
        
        self.FireDe.set_active(Config.getboolean("firewire", "detected"))
        self.FireEn.set_active(Config.getboolean("firewire", "enabled"))
        self.FireMod.set_text(Config.get("firewire", "modules"))
        
        self.CamDe.set_active(Config.getboolean("webcam", "detected"))
        self.CamEn.set_active(Config.getboolean("webcam", "enabled"))
        self.CamMod.set_text(Config.get("webcam", "modules"))
        
        self.TrackerDe.set_active(Config.getboolean("tracker", "detected"))
        self.TrackerEn.set_active(Config.getboolean("tracker", "enabled"))
        
        #other tab
        self.ATIDe.set_active(Config.getboolean("ati", "detected"))
        self.ATIEn.set_active(Config.getboolean("ati", "enabled"))
        self.ATISav.set_text(Config.get("ati", "saving_setting"))
        self.ATIPerf.set_text(Config.get("ati", "performance_setting"))
        
        self.NVDe.set_active(Config.getboolean("nvidia", "detected"))
        self.NVEn.set_active(Config.getboolean("nvidia", "enabled"))
        self.NVSav.set_text(Config.get("nvidia", "saving_setting"))
        self.NVPerf.set_text(Config.get("nvidia", "performance_setting"))
        
        self.MCDe.set_active(Config.getboolean("sched_mc", "detected"))
        self.MCEn.set_active(Config.getboolean("sched_mc", "enabled"))
        self.MCSav.set_text(Config.get("sched_mc", "saving_setting"))
        self.MCPerf.set_text(Config.get("sched_mc", "performance_setting"))
        
        self.WMDe.set_active(Config.getboolean("window_manager", "detected"))
        self.WMEn.set_active(Config.getboolean("window_manager", "enabled"))
        self.WMSav.set_text(Config.get("window_manager", "saving_setting"))
        self.WMPerf.set_text(Config.get("window_manager", "performance_setting"))
        
        self.ASPMDe.set_active(Config.getboolean("pcie_aspm", "detected"))
        self.ASPMEn.set_active(Config.getboolean("pcie_aspm", "enabled"))
        self.ASPMSav.set_text(Config.get("pcie_aspm", "saving_setting"))
        self.ASPMPerf.set_text(Config.get("pcie_aspm", "performance_setting"))
        
        self.IntelSDe.set_active(Config.getboolean("intel_sound", "detected"))
        self.IntelSEn.set_active(Config.getboolean("intel_sound", "enabled"))
        self.IntelSSav.set_text(Config.get("intel_sound", "saving_setting"))
        self.IntelSPerf.set_text(Config.get("intel_sound", "performance_setting"))
        
        self.AC97De.set_active(Config.getboolean("ac97_sound", "detected"))
        self.AC97En.set_active(Config.getboolean("ac97_sound", "enabled"))
        self.AC97Sav.set_text(Config.get("ac97_sound", "saving_setting"))
        self.AC97Perf.set_text(Config.get("ac97_sound", "performance_setting"))
        
        self.BackDe.set_active(Config.getboolean("xbacklight", "detected"))
        self.BackEn.set_active(Config.getboolean("xbacklight", "enabled"))
        self.BackSav.set_text(Config.get("xbacklight", "saving_setting"))
        self.BackPerf.set_text(Config.get("xbacklight", "performance_setting"))
        
        self.ACPITDe.set_active(Config.getboolean("acpi_therm", "detected"))
        self.ACPITEn.set_active(Config.getboolean("acpi_therm", "enabled"))
        self.ACPITSav.set_text(Config.get("acpi_therm", "saving_setting"))
        self.ACPITPerf.set_text(Config.get("acpi_therm", "performance_setting"))
        
        self.NMIDe.set_active(Config.getboolean("nmi_watchdog", "detected"))
        self.NMIEn.set_active(Config.getboolean("nmi_watchdog", "enabled"))
        self.NMISav.set_text(Config.get("nmi_watchdog", "saving_setting"))
        self.NMIPerf.set_text(Config.get("nmi_watchdog", "performance_setting"))
        
        # One time use tab
        self.IntUp_interfaces = Config.get("config", "interfaces").split(",")
        for self.IntUp_interface in self.IntUp_interfaces:
            self.IntUp_row=self.IntUpListStore.insert_after(None, None)
            self.IntUpListStore.set_value(self.IntUp_row, 0, self.IntUp_interface)
        self.IntUpCombo.set_model(self.IntUpListStore)
        self.IntUp_cell=gtk.CellRendererText()
        self.IntUpCombo.pack_start(self.IntUp_cell)
        self.IntUpCombo.add_attribute(self.IntUp_cell, 'text', 0)
        self.IntUpCombo.set_wrap_width(1)
                
    def PrefsCancelClicked(self, widget):
        "cancel the preferences window"
        
        self.advPrefsWindow.destroy()
        
    def PrefsOKClicked(self, widget):
        "apply preferences"
        
        #general tab
        Config.set("config","username", self.username.get_text())
        Config.set("config","root_drive", self.rootDrive.get_text())
        Config.set("config","root_partition", self.rootPartition.get_text())
        Config.set("config","cdrom", self.cdrom.get_text())
        Config.set("config","interfaces", self.interfaces.get_text())
        Config.set("config","first_run", str(self.firstRun.get_active()))
        Config.set("config","smp", str(self.smp.get_active()))
        Config.set("config","notifications", str(self.notifications.get_active()))
        
        #disks tab
        Config.set("dirty_ratio", "detected", str(self.DRatioDe.get_active()))
        Config.set("dirty_ratio", "enabled", str(self.DRatioEn.get_active()))
        Config.set("dirty_ratio", "saving_setting", self.DRatioSav.get_text())
        Config.set("dirty_ratio", "performance_setting", self.DRatioPerf.get_text())
        
        Config.set("dirty_background_ratio", "detected", str(self.DBRatioDe.get_active()))
        Config.set("dirty_background_ratio", "enabled", str(self.DBRatioEn.get_active()))
        Config.set("dirty_background_ratio", "saving_setting", self.DBRatioSav.get_text())
        Config.set("dirty_background_ratio", "performance_setting", self.DBRatioPerf.get_text())
        
        Config.set("dirty_writeback", "detected", str(self.DWritebackDe.get_active()))
        Config.set("dirty_writeback", "enabled", str(self.DWritebackEn.get_active()))
        Config.set("dirty_writeback", "saving_setting", self.DWritebackSav.get_text())
        Config.set("dirty_writeback", "performance_setting", self.DWritebackPerf.get_text())
        
        Config.set("hdparm", "detected", str(self.HDPDe.get_active()))
        Config.set("hdparm", "enabled", str(self.HDPEn.get_active()))
        Config.set("hdparm", "saving_setting", self.HDPSav.get_text())
        Config.set("hdparm", "performance_setting", self.HDPPerf.get_text())
        
        Config.set("journal_commit", "detected", str(self.JCommitDe.get_active()))
        Config.set("journal_commit", "enabled", str(self.JCommitEn.get_active()))
        Config.set("journal_commit", "saving_setting", self.JCommitSav.get_text())
        Config.set("journal_commit", "performance_setting", self.JCommitPerf.get_text())
        
        Config.set("sata", "detected", str(self.SataDe.get_active()))
        Config.set("sata", "enabled", str(self.SataEn.get_active()))
        Config.set("sata", "saving_setting", self.SataSav.get_text())
        Config.set("sata", "performance_setting", self.SataPerf.get_text())
        Config.set("sata", "hosts", self.SataHosts.get_text())
        
        Config.set("hal_polling", "detected", str(self.HALDe.get_active()))
        Config.set("hal_polling", "enabled", str(self.HALEn.get_active()))
        
        Config.set("readahead", "detected", str(self.ReadADe.get_active()))
        Config.set("readahead", "enabled", str(self.ReadAEn.get_active()))
        Config.set("readahead", "saving_setting", self.ReadASav.get_text())
        Config.set("readahead", "performance_setting", self.ReadAPerf.get_text())
        
        #network tab
        Config.set("iwl_wifi", "detected", str(self.IWLDe.get_active()))
        Config.set("iwl_wifi", "enabled", str(self.IWLEn.get_active()))
        Config.set("iwl_wifi", "saving_setting", self.IWLSav.get_text())
        Config.set("iwl_wifi", "performance_setting", self.IWLPerf.get_text())
        Config.set("iwl_wifi", "dirs", self.IWLDirs.get_text())
        
        Config.set("ipw_wifi", "detected", str(self.IPWDe.get_active()))
        Config.set("ipw_wifi", "enabled", str(self.IPWEn.get_active()))
        Config.set("ipw_wifi", "saving_setting", self.IPWSav.get_text())
        Config.set("ipw_wifi", "performance_setting", self.IPWPerf.get_text())
        if self.IPWCombo.get_active_text():
            Config.set("ipw_wifi", "interface", self.IPWCombo.get_active_text())
        
        #modules tab
        Config.set("bluetooth", "detected", str(self.BlueDe.get_active()))
        Config.set("bluetooth", "enabled", str(self.BlueEn.get_active()))
        Config.set("bluetooth", "modules", self.BlueMod.get_text())
        
        Config.set("usb", "detected", str(self.USBDe.get_active()))
        Config.set("usb", "enabled", str(self.USBEn.get_active()))
        Config.set("usb", "modules", self.USBMod.get_text())
        
        Config.set("firewire", "detected", str(self.FireDe.get_active()))
        Config.set("firewire", "enabled", str(self.FireEn.get_active()))
        Config.set("firewire", "modules", self.FireMod.get_text())
        
        Config.set("webcam", "detected", str(self.CamDe.get_active()))
        Config.set("webcam", "enabled", str(self.CamEn.get_active()))
        Config.set("webcam", "modules", self.CamMod.get_text())
        
        Config.set("tracker", "detected", str(self.TrackerDe.get_active()))
        Config.set("tracker", "enabled", str(self.TrackerEn.get_active()))
        
        #other tab
        Config.set("ati", "detected", str(self.ATIDe.get_active()))
        Config.set("ati", "enabled", str(self.ATIEn.get_active()))
        Config.set("ati", "saving_setting", self.ATISav.get_text())
        Config.set("ati", "performance_setting", self.ATIPerf.get_text())
        
        Config.set("nvidia", "detected", str(self.NVDe.get_active()))
        Config.set("nvidia", "enabled", str(self.NVEn.get_active()))
        Config.set("nvidia", "saving_setting", self.NVSav.get_text())
        Config.set("nvidia", "performance_setting", self.NVPerf.get_text())
        
        Config.set("sched_mc", "detected", str(self.MCDe.get_active()))
        Config.set("sched_mc", "enabled", str(self.MCEn.get_active()))
        Config.set("sched_mc", "saving_setting", self.MCSav.get_text())
        Config.set("sched_mc", "performance_setting", self.MCPerf.get_text())
        
        Config.set("window_manager", "detected", str(self.WMDe.get_active()))
        Config.set("window_manager", "enabled", str(self.WMEn.get_active()))
        Config.set("window_manager", "saving_setting", self.WMSav.get_text())
        Config.set("window_manager", "performance_setting", self.WMPerf.get_text())
        
        Config.set("pcie_aspm", "detected", str(self.ASPMDe.get_active()))
        Config.set("pcie_aspm", "enabled", str(self.ASPMEn.get_active()))
        Config.set("pcie_aspm", "saving_setting", self.ASPMSav.get_text())
        Config.set("pcie_aspm", "performance_setting", self.ASPMPerf.get_text())
        
        Config.set("intel_sound", "detected", str(self.IntelSDe.get_active()))
        Config.set("intel_sound", "enabled", str(self.IntelSEn.get_active()))
        Config.set("intel_sound", "saving_setting", self.IntelSSav.get_text())
        Config.set("intel_sound", "performance_setting", self.IntelSPerf.get_text())
        
        Config.set("ac97_sound", "detected", str(self.AC97De.get_active()))
        Config.set("ac97_sound", "enabled", str(self.AC97En.get_active()))
        Config.set("ac97_sound", "saving_setting", self.AC97Sav.get_text())
        Config.set("ac97_sound", "performance_setting", self.AC97Perf.get_text())
        
        Config.set("xbacklight", "detected", str(self.BackDe.get_active()))
        Config.set("xbacklight", "enabled", str(self.BackEn.get_active()))
        Config.set("xbacklight", "saving_setting", self.BackSav.get_text())
        Config.set("xbacklight", "performance_setting", self.BackPerf.get_text())
        
        Config.set("acpi_therm", "detected", str(self.ACPITDe.get_active()))
        Config.set("acpi_therm", "enabled", str(self.ACPITEn.get_active()))
        Config.set("acpi_therm", "saving_setting", self.ACPITSav.get_text())
        Config.set("acpi_therm", "performance_setting", self.ACPITPerf.get_text())
        
        Config.set("nmi_watchdog", "detected", str(self.NMIDe.get_active()))
        Config.set("nmi_watchdog", "enabled", str(self.NMIEn.get_active()))
        Config.set("nmi_watchdog", "saving_setting", self.NMISav.get_text())
        Config.set("nmi_watchdog", "performance_setting", self.NMIPerf.get_text())
        
        Config.write(open("/etc/wattospm/config","w"))  
        self.advPrefsWindow.destroy() 
        
    def RestoreDefaultsClicked(self,widget):
        "restore default settings"
        
        global Config
        Config = ConfigParser.ConfigParser()
        oldConfig = open("/etc/wattospm/config","w")
        defaultConfig = open("/etc/wattospm/config.default", "r")
        oldConfig.write(defaultConfig.read())
        oldConfig.close()
        defaultConfig.close()
        ConfigFile = open("/etc/wattospm/config")
        Config.readfp(ConfigFile)
        ConfigFile.close()
        Config.set("config", "first_run", "False")
        self.readSettings()
                
class firelauncher:
    
    def cancel(self, widget, data):
        self.firewindow.destroy()
        
    def launch(self,  widget, data):
        if not os.path.exists("/dev/shm/%s" % (self.user,)):
            os.system("gksudo mkdir /dev/shm/%s" % (self.user,))
            os.system("gksudo chown %s:%s /dev/shm/%s" % (self.user,self.user,self.user))
        if os.path.islink(self.FirefoxFolder) and os.path.exists(self.FirefoxFolderBackup):
            os.remove(self.FirefoxFolder)
            shutil.copytree(self.FirefoxFolderBackup,self.FirefoxFolder)
        if not os.path.exists(self.FirefoxFolderBackup):
            shutil.copytree(self.FirefoxFolder,self.FirefoxFolderBackup)
        if os.path.exists(self.FirefoxRam):
            shutil.rmtree(self.FirefoxRam)
        shutil.move(self.FirefoxFolder,self.FirefoxRam)
        os.symlink(self.FirefoxRam,self.FirefoxFolder)
        FirefoxProcess = sp.Popen("firefox")
        self.firewindow.destroy()
        self.check_status()
        if Config.getboolean("config", "notifications"): 
            pynotify.init("WattOS Power Manager") 
            n = pynotify.Notification("Firefox launched") 
            n.attach_to_status_icon(icon) 
            n.show() 
    
    def check_status(self):
        self.PSProcess = sp.Popen(["ps","aux"],stdout=sp.PIPE)
        self.result = self.PSProcess.stdout.read()
        if "firefox" in self.result:
            if (time.localtime()[4] % 5 == 0 and time.localtime()[5] < 3):
                print "firefox folder backed up"
                sp.Popen(["rsync","-a","--delete",self.FirefoxRam,self.FirefoxFolderBackup])
            gobject.timeout_add(1000, self.check_status)
        else:
            self.closer()
    
    def closer(self):
        os.remove(self.FirefoxFolder)
        shutil.move(self.FirefoxRam,self.FirefoxFolder)
        shutil.rmtree(self.FirefoxFolderBackup)
        if Config.getboolean("config", "notifications"): 
            pynotify.init("WattOS Power Manager") 
            n = pynotify.Notification("Firefox profile restored") 
            n.attach_to_status_icon(icon) 
            n.show() 
                  
    def get_mem_needed(self):
        return commands.getoutput("du -sh /home/%s/.mozilla/firefox/" % (Config.get("config", "username"),)).split()[0]
    
    def get_mem_free(self):
        for line in commands.getoutput("free -m").split("\n"):
            if "Mem" in line:
                return line.split()[3] + "M"
                
    def __init__(self, data=None):
        self.user = Config.get("config", "username")
        self.FirefoxFolder = "/home/%s/.mozilla/firefox" % (self.user,)
        self.FirefoxFolderBackup = "/home/%s/.mozilla/firefoxbackup" % (self.user,)
        self.FirefoxRam = "/dev/shm/%s/firefox" % (self.user,)
        self.memNeeded = self.get_mem_needed()
        self.memFree = self.get_mem_free()
        
        #Create confirmation window
        self.firewindow = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.firewindow.set_title("Launch Firefox?")
        self.firewindow.set_resizable(False)
        self.firewindow.set_position(1)
        self.firewindow.connect("delete_event" , self.cancel)
        self.firewindow.set_border_width(20)

        # Create a box to pack widgets into
        self.firevbox = gtk.VBox(False, 0)
        self.firehbox = gtk.HBox(False, 0)
        self.firewindow.add(self.firevbox)
        
        # Create Label
        self.firelabel = gtk.Label("Memory needed: %s Memory Available: %s" % (self.memNeeded,self.memFree))
        self.firevbox.pack_start(self.firelabel, True, True, 0)
        self.firevbox.pack_start(self.firehbox, True, True, 0)
        self.firelabel.show()
        
        # Create ok button
        self.fireok = gtk.Button("_Ok")
        self.fireok.set_use_underline(True)
        self.fireok.set_border_width(10)
        self.fireok.connect("clicked", self.launch, "launch")
        self.firehbox.pack_start(self.fireok, True, True, 0)
        self.fireok.show()
        
        # Create cancel button
        self.firecancel = gtk.Button("_Cancel")
        self.firecancel.set_use_underline(True)
        self.firecancel.set_border_width(10)
        self.firecancel.connect("clicked", self.cancel, "cancel")
        self.firehbox.pack_start(self.firecancel, True, True, 0)
        self.firecancel.show()
        
        self.firevbox.show()
        self.firehbox.show()
        self.firewindow.show()
    
def exit_app(data=None):
    "exit application"
    
    gtk.main_quit()

def make_menu(event_button, event_time, data=None):
    "make the right click menu"
    
    menu = gtk.Menu()
    
    manual_item = gtk.MenuItem("Manual")
    auto_item = gtk.MenuItem("Auto")
    menu.append(manual_item)
    menu.append(auto_item)
    manual_item.connect_object("activate", change_activation_mode, "manual")
    auto_item.connect_object("activate", change_activation_mode, "auto")
    manual_item.show()
    auto_item.show()
    
    separator1 = gtk.MenuItem()
    menu.append(separator1)
    separator1.show()
    
    #show the manual options if set to manual mode 
    if Config.get("config", "activation") == "manual":
        power_saving_item = gtk.MenuItem("Power Saving")
        performance_item = gtk.MenuItem("performance")
        menu.append(power_saving_item)
        menu.append(performance_item)
        power_saving_item.connect_object("activate", request_power_mode , "saving")
        performance_item.connect_object("activate", request_power_mode, "performance")
        power_saving_item.show()
        performance_item.show()
        separator2 = gtk.MenuItem()
        menu.append(separator2)
        separator2.show()
    
    #firefox launcher menu item
    firefox_item = gtk.MenuItem("Launch Firefox")
    menu.append(firefox_item)
    firefox_item.connect_object("activate", firelauncher, "launch")
    firefox_item.show()
    seperator3 = gtk.MenuItem()
    menu.append(seperator3)
    seperator3.show()
        
    #show the exit option no matter what is configured
    exit_item = gtk.MenuItem("Exit")
    menu.append(exit_item)
    exit_item.connect_object("activate", exit_app, "Exit App")
    exit_item.show()
 
    #Popup the menu
    menu.popup(None, None, None, event_button,
        event_time)
 
def on_right_click(data, event_button, event_time):
    "set the right click tray icon menu"
    
    make_menu(event_button, event_time) 

def on_left_click(event):
    "make the preferences menu"

    Preferences = advPrefs() 

def change_activation_mode(mode):
    "change activation mode"
    
    if mode == "manual":
        Config.set("config", "activation", "manual")
    else:
        Config.set("config", "activation", "auto")
        
    Config.write(open("/etc/wattospm/config","w"))
    
def request_power_mode(mode):
    "change power mode"
    
    Config.set("config", "requested_mode", mode)
    Config.write(open("/etc/wattospm/config","w"))
    
def check_mode(lastMode):
    "check what is the current mode and notify"
    
    #open config file
    global Config
    Config = ConfigParser.ConfigParser()
    ConfigFile = open("/etc/wattospm/config")
    Config.readfp(ConfigFile)
    ConfigFile.close()
    
    currentMode = Config.get("config", "current_mode")
    
    if not lastMode == currentMode:
        icon.set_from_file("/usr/share/wattospm/%s.png" % (currentMode,))
        
        if Config.getboolean("config", "notifications"): 
            pynotify.init("WattOS Power Manager") 
            n = pynotify.Notification("Power Mode Changed", "%s" % (currentMode.replace("_", " "),)) 
            n.attach_to_status_icon(icon) 
            n.show() 
            
    gobject.timeout_add(1000, check_mode, currentMode)
        
if __name__ == "__main__":
    "main section of program"
    
    # Set icon, right and left click menus
    icon = gtk.status_icon_new_from_file("/usr/share/wattospm/stock.png")
    icon.connect('popup-menu', on_right_click)
    icon.connect('activate', on_left_click)

    #open config file
    global Config
    Config = ConfigParser.ConfigParser()
    ConfigFile = open("/etc/wattospm/config")
    Config.readfp(ConfigFile)
    ConfigFile.close()
    
    #set display environment variable
    Config.set("config","display", os.getenv("DISPLAY"))
    Config.write(open("/etc/wattospm/config","w"))

    check_mode("unknown")    
    
    gtk.main()
